import {optTypes} from "@/utils/const.js";
import {approvalUserTypes} from "./const.js";

function All() {}
All.prototype = {
    timer: "",
    treeRecursion(data) {
        for (let key in data) {
            data.value = data.id;
            if (typeof data[key] === "object") this.treeRecursion(data[key]);
        }
        return data;
    },
    arrToStr(arr) {
        if (arr) {
            return arr.map(item => { return item.name }).toString()
        }
    },
    toggleClass(arr, elem, key = 'id') {
        return arr.some(item => { return item[key] == elem[key] });
    },
    toChecked(arr, elem, key = 'id') {
        var isIncludes = this.toggleClass(arr, elem, key);
        !isIncludes ? arr.push(elem) : this.removeEle(arr, elem, key);
    },
    removeEle(arr, elem, key = 'id') {
        var includesIndex;
        arr.map((item, index) => {
            if (item[key] == elem[key]) {
                includesIndex = index
            }
        });
        arr.splice(includesIndex, 1);
    },
    setApproverStr(nodeConfig) {
        let approvalUserType =nodeConfig.approvalUserType
        console.log(approvalUserType)
        console.log(nodeConfig.nodeApproveList)
        if (['1','4','6'].includes(approvalUserType) && nodeConfig.nodeApproveList.length===0){
            return  "请选择"
        }
        let o = approvalUserTypes.find(item=>item.value === approvalUserType)
        console.log(o)
        if (!o){
            return "请选择"
        }
        return o.label
    },
    dealStr(str, obj) {
        let arr = [];
        let list = str.split(",");
        for (var elem in obj) {
            list.map(item => {
                if ((item -1) == elem) {
                    arr.push(obj[elem].value)
                }
            })
        }
        return arr.join("或")
    },
    getLabelStr(index, obj) {
        if(!obj) return;
        let ret = obj[index -1];
        if (ret) {
            return ret.value;
        }
        return '';
    },
    conditionStr(nodeConfig, index) {
        let { conditionList, nodeApproveList } = nodeConfig.conditionExpressionList[index];
        console.log(conditionList, nodeApproveList)
        if (conditionList.length === 0) {
            return (index === nodeConfig.conditionExpressionList.length - 1) && nodeConfig.conditionExpressionList[index].conditionList.length === 0 ? '其他条件进入此流程' : '请设置条件'
        } else {
            let str_arr = []
            for (let i = 0; i < conditionList.length; i++) {
                let { opt={},optType,value } = conditionList[i];
                let {label,name,optionItems,columnType}= opt;
                let val = "";
                if (optionItems){// checkbox  radio select
                    let v= Array.isArray(value) ? value :[value];
                    let h=[]
                    optionItems.forEach(item=>{
                        if (v.includes(item.value)) {
                            h.push(item.label)
                        }
                    })
                    val= h.join(",")
                }else {
                    val= value;
                }
                let s= optTypes.find(item=>item.value===optType);
                str_arr.push(`${label}${s.label}${val}`)
            }
            return str_arr.length ? str_arr.join(',') : '请设置条件'
        }
    },
    copyerStr(nodeConfig) {
        let approvalUserType =nodeConfig.approvalUserType
        console.log(approvalUserType)
        console.log(nodeConfig.nodeApproveList)
        if (['1','4','6'].includes(approvalUserType) && nodeConfig.nodeApproveList.length===0){
            return  "请选择"
        }
        let o = approvalUserTypes.find(item=>item.value === approvalUserType)
        console.log(o)
        if (!o){
            return "请选择"
        }
        return o.label
    },
}

export default new All();

